<?php
/**
 * 工单支付
 * User: Administrator
 * Date: 2018/8/6/006
 * Time: 21:18
 */

namespace app\lib\Order\OrderOption;


use app\lib\Flow\FlowLog;
use app\lib\Order\IsOrder\IsOrder;
use app\lib\Order\OrderExtensionConfig\GetOrderBindingExtension;
use app\lib\Order\OrderExtensionConfig\OrderBindingEditExtension;
use app\lib\Order\OrderInfo\GetOrderMoney;
use app\lib\Order\OrderMoney\AddOrderMoneyRecord;
use app\lib\Order\OrderMoney\EditOrderMoneyRecord;
use app\lib\Order\OrderNotice\PaymentNotice;
use app\lib\Order\OrderPaymentNotice\AddOrderNotice;

class OrderPayOption
{
    protected $orderId;
    protected $moneyId;
    protected $payId;
    protected $payType;
    protected $isAdmin = 0;
    protected $content;
    protected $flowDetails;
    public function __construct($orderId,$moneyId)
    {
        $this->orderId = $orderId;
        $this->moneyId = $moneyId;
    }

    public function setPayId($payId){
        $this->payId = $payId;
        return $this;
    }

    public function setPayType($payType){
        $this->payType = $payType;
        return $this;
    }

    public function setIsAdmin($isAdmin){
        $this->isAdmin = $isAdmin == 1?1:0;
        return $this;
    }

    /**
     *
     * @return bool
     * @throws \Exception
     */
    protected function check(){
        if(empty($this->payId) || !is_numeric($this->payId)){
            exception('未获取到支付人');
        }
        if(empty($this->payType) || !in_array($this->payType,['alipay','weixin','other','admin'])){
            exception('未获取支付类型');
        }
        if(!(new IsOrder($this->orderId))->isExist()){
            exception('工单状态异常无法支付');
        }
        return true;
    }

    /**
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save(){
        $this->check();
        /*获取交款期数id*/
        $info =  (new GetOrderMoney($this->orderId,false))
            ->setMoneyId($this->moneyId)->getFindInfo();
        $this->payHandle($info);
        /* 写入工单流水和工单日志*/
        $this->writeFlowAndOrderLog($info);
        /*标签处理*/
        $this->tagHandle();
        return true;
    }

    /**
     * 交款处理
     * @param $info
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function payHandle($info){
        if($info['status'] == 1){
            exception('该期已经交款,请勿重复交款');
        }
        /*查询是否生成记录 未生成记录*/
        if($info['status'] == null){
            $noticeModel = new AddOrderNotice();
            /*生成交款通知记录*/
            $noticeModel->setOpUid($this->payId)->setOrderId($this->orderId)
                ->setMoneyId($this->moneyId)->setContent('主动交款记录,工单['.$info['name'].']')->AddOrderNoPayNotice();
            /*生成支付信息*/
            (new AddOrderMoneyRecord())->setOrderMoneyId($info['id'])
                ->setPayType($this->payType)->setPayUserId($this->payId)->pay();
        }
        /*如果记录存在 改变状态*/
        if($info['status'] == 0 && $info['status']!=null){
            (new EditOrderMoneyRecord())->setOrderMoneyId($info['id'])
                ->setPayType($this->payType)->setPayUserId($this->payId)->changePay();
        }
    }

    /**
     * 写入工单流水和工单日志
     * @param $info
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function writeFlowAndOrderLog($info){
        /*实例化流水*/
        $flow = (new FlowLog($this->payId));
        /*记录流水*/
        $flow->setPayType('admin')->setOrderMoneyId($this->moneyId)->setOrderId($this->orderId)
            ->setMoney($info['money'])->setFlowDetails('工单支付，工单【'.$info['name'].'】')
            ->setFlowContent('工单款')->setFlowType(1)->setIsAdmin($this->isAdmin)->save();
        /*记录工单日志*/
        $log =  (new OrderOptionLog($this->orderId));
        $log->setOptionType('PAYMONEY');
        $log->setOptionUserId($this->payId);
        $log->setOptionContent('缴纳了工单款，工单【'.$info['name'].'】');
        $log->save();
        /*发送通知内部监控通知*/
        (new PaymentNotice($this->orderId,$info['id']))->save();
        return true;
    }

    /**
     * 解除绑定标签
     * @return bool
     * @throws \Exception
     */
    protected function tagHandle(){
        $model =  new GetOrderBindingExtension($this->orderId);
        if(!$model->isOrderBindingPay()){
            return true;
        }
        $orderBinding = new OrderBindingEditExtension($this->orderId,'pay','完成支付,自动取消');
        $orderBinding->releaseBinding();
        return true;
    }
}